<?php
namespace Home\Controller;
use Think\Controller;
class AccountController extends CommonController {
    public $appkey = MOB_APPK;

    /**
     * 邮箱验证
     */
    public function mailverify(){

        $ucode = $_GET['uid'];
        $hash = $_GET['hash'];
        if(!$ucode) {
            echo '<script>alert("验证失败")';die;
        }
        if(!$hash) {
            echo '<script>alert("验证失败")';die;
        }
        $ucode = base64_decode($ucode);
        list($uid,$email) =  explode(' ', $ucode);

        $member_common = M('member_common');
        $where['member_id'] = $uid;
        $result = $member_common->where($where)->find();
        if(!$member_common) {
            echo '<script>alert("验证失败")';die;
        }

        if($result['send_acode_time'] < (time()-86400)){
            echo '<script>alert("链接已失效")';die;
        }
        if(md5($result['auth_code']) != $hash){
            echo '<script>alert("验证失败")';die;
        }
        $member = M('member');
        $con['member_email'] = $email;
        $con['member_email_bind'] = 1;
        $update = $member->where("member_id = $uid")->save($con);
        if(!$update){
            echo '<script>alert("验证失败")';die;
        }
        $data['auth_code'] = '';
        $data['send_acode_time'] = 0;
        $update = $member_common->where("member_id = $uid")->save($data);
        if(!$update){
            echo '<script>alert("验证失败")';die;
        }
        echo '<script>alert("验证成功")';die;
    }

    /**
     * @param $length
     * @param int $numeric
     * @return string
     */
    function random($length, $numeric = 0) {
        $seed = base_convert(md5(print_r($_SERVER, 1).microtime()), 16, $numeric ? 10 : 35);
        $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
        $hash = '';
        $max = strlen($seed) - 1;
        for($i = 0; $i < $length; $i++) {
            $hash .= $seed[mt_rand(0, $max)];
        }
        return $hash;
    }

    /**
     * 绑定邮箱
     */
	public function bindmail(){
        Vendor('phpmail/email');
        $uid = I('post.uid');
        $email = $_POST['toemail'];
        if(!$uid) returnApiError(1,'用户id参数错误');
        if(!$email) returnApiError(1,'请填写邮箱地址');
        $seed = $this->random(6);
        $ucode = base64_encode($uid.' '.$email);
        $verify_url = 'http://un-eagle.cn/api/index.php/Home/Account/mailverify.html?uid='.$ucode.'&hash='.md5($seed);

        $data['auth_code'] = $seed;
        $data['send_acode_time'] = time();
        $member = M('member');
        $member_common = M('member_common');

        if(!$member->find($uid)){
            returnApiError(1,'会员不存在');
        }

        if(!$member_common->find($uid)){
            $add['member_id'] = $uid;
            $member_common->add($add);
        }

        if($member->where("member_email = '$email'")->find()){
            returnApiError(1,'邮箱已被使用');
        }
        if(!$member_common->where("member_id = $uid")->save($data)){
           //echo $member_common->getLastSql();
            returnApiError(1,'系统发生错误，如有疑问请与管理员联系');
        }

       // echo "<pre>";
        //print_r($setting);
        $content = '<p>您好！</p><p>请在24小时内点击以下链接完成邮箱验证</p><p><a href="'.$verify_url.'" target="_blank">马上验证</a></p><p>如果您不能点击上面链接，还可以将以下链接复制到浏览器地址栏中访问</p><p> '.$verify_url.' </p>';
        $title = '邮箱验证通知';
        $smtpserver = 'smtp.163.com';//SMTP服务器
        $smtpserverport = 25;//SMTP服务器端口
        $smtpusermail = '18692255303@163.com';//SMTP服务器的用户邮箱
        $smtpemailto = $email;//发送给谁
        $smtpuser = '18692255303@163.com';//SMTP服务器的用户帐号
        $smtppass = 'admin123';//SMTP服务器的用户密码
        $mailtitle = $title;//邮件主题
        $mailcontent = $content ; //邮件内容
        $mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
        //************************ 配置信息 ****************************

        $smtp = new \smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
        $smtp->debug = false;//是否显示发送的调试信息
       // print_r($smtp);
        $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);

        if($state==""){
           returnApiError(1,'邮箱发送失败');
        }
        returnApiSuccess('邮箱发送成功');


    }

    /**
     * 绑定手机
     */
    public function bindtel(){
        $uid = I('post.uid',0,'intval');
        $tel = $_POST['tel'];
        $code = $_POST['code'];
        $pass = $_POST['pass'];
        if(!$tel)  returnApiError(1,'请填写手机号');
        if(!$code)  returnApiError(1,'请填写验证码');
        if(!$pass)  returnApiError(1,'请填写登录密码');
        $member = M('member');
        $result = $member->find($uid);
        if(!$result){
            returnApiError(1,'用户不存在');
        }

        $pass = md5($pass);
        if(!$member->where("member_passwd = '$pass'")->find()){
            returnApiError(1,'登录密码不正确');
        }
        $where['member_mobile'] = $tel;
        if($member->where($where)->find()){
            returnApiError(1,'手机号已被使用');
        }

        //短信验证
        $api = 'https://webapi.sms.mob.com/sms/verify';
        $arr = array(
            'appkey' => $this->appkey,
            'phone' => $tel,
            'zone' => '86',
            'code' => $code,
        );
        $response = $this->postRequest($api, $arr);
        $param = json_decode($response,true);
        $status= $param['status'];
        if($status != 200){
            returnApiError($status,'短信校验失败');
        }

        $con['member_mobile'] = $tel;
        $con['member_mobile_bind'] = 1;
        if($member->where("member_id = $uid")->save($con)){
            returnApiSuccess('绑定成功');
        }else{
            returnApiSuccess('绑定失败');
        }

    }

    /**
     * 解绑
     */
    public function unwrap(){
        $uid = I('post.uid',0,'intval');
        $state = I('post.state',0,'intval');
        if(!$state) returnApiError(1,'state参数错误');
        $member = M('member');
        $result = $member->find($uid);
        if(!$result) returnApiError(1,'会员不存在');

        //qq解绑
        if($state == 1){
            $con['member_qqopenid'] = '';
            $con['member_qqinfo'] = '';
        }

        //微信解绑
        if($state == 2){
            $con['weixin_unionid'] = '';
            $con['weixin_info'] = '';
        }
        //微信解绑
        if($state == 3){
            $con['member_sinaopenid'] = '';
            $con['member_sinainfo'] = '';
        }

        if(!$con) returnApiError(1,'系统错误');
        if($member->where("member_id = $uid")->save($con)){
            returnApiSuccess('解绑成功');
        }else{
            returnApiSuccess('解绑失败');
        }
    }



}